<template>
  <div class="ercdeo">
    <vue-qr v-if="qrcodeVal" :text="qrcodeVal" :size="200"></vue-qr>
    <bindcode
      @loginSuccess="loginSuccess"
      type="phone"
      ref="bindcodebox"
    ></bindcode>
  </div>
</template>

<script>
import bindcode from "@/components/bindcode.vue";
import { ElMessage } from "element-plus";
// import { checklogin } from "@/axios/community.js";
import { tick, scantick, userinfo, getToken } from "@/axios/api.js";
import vueQr from "vue-qr/src/packages/vue-qr.vue";

import { useRouter } from "vue-router";
import {
  computed,
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  getCurrentInstance,
  onUnmounted,
  ref,
  watch,
} from "vue";
import { useStore } from "vuex";
export default defineComponent({
  components: {
    vueQr,
    bindcode,
  },
  setup(props, { attrs, slots, emit }) {
    const bindcodebox = ref(null);
    let codeinfo = computed(() => {
      if (store.state.codeinfo) {
        return JSON.parse(store.state.codeinfo);
      } else {
        return "";
      }
    });
    const router = useRouter();
    const { proxy } = getCurrentInstance();
    let store = useStore();
    let data = reactive({
      qrcodeVal: "",
      time: "",
      times: 3000,
      TimeNum: 0,
    });
    let getuserinfo = () => {
      userinfo().then((res) => {
        let dataslist = res.data;
        dataslist.mininfo = store.state.minUserInfo;
        store.commit("saveUserInfo", dataslist);
        console.log(dataslist, "扫码登录");
        emit("handleClose");
      });
    };

    // 获取创作token
    let getCreatToken = () => {
      getToken().then((res) => {
        let mjToken = res.data;
        store.commit("setMjToken", mjToken);
      });
    };
    // 登录成功
    let loginSuccess = (res, type) => {
      clearInterval(data.time);
      store.commit("setcodeinfo", "");
      store.commit("saveToken", res.data.accessToken);
      store.commit("setminToken", res.data.miniData.token);
      getCreatToken();
      store.commit("saveminUserInfo", res.data.miniData.user);
      store.dispatch("updataMinUserBalance");
      getuserinfo();

      ElMessage.success("登录成功");
    };

    // 检查是否登录
    let getscantick = (code) => {
      let id = "?code=" + code;
      checklogin()
        .then((res) => {
          if (res.code == 0) {
            loginSuccess(res);
          } else if (res.code == 2) {
            // 无扫码
          } else if (res.code == 3) {
            clearInterval(data.time);
            store.commit("setcodeinfo", "");
            bindcodebox.value.dialogClick(res.data);
          }
        })
        .catch((err) => {
          clearInterval(data.time);
        });
    };
    // 获取二维码
    let gettick = () => {
      tick().then((res) => {
        let info = JSON.parse(res.data.data);
        data.qrcodeVal = info.url;
        let codeinfos = {};
        let nowTime = new Date().getTime() / 1000;
        let secon = info.expire_seconds;
        codeinfos.nowTime = Math.floor(nowTime);
        codeinfos.endTime = Math.floor(nowTime + secon);
        codeinfos.url = info.url;
        codeinfos.scene_str = res.data.scene_str;
        store.commit("setcodeinfo", codeinfos);
        data.time = setInterval(() => {
          getscantick(codeinfos.scene_str);
        }, data.times);
      });
    };
    let watchTime = () => {
      if (codeinfo.value.nowTime) {
        data.TimeNum = codeinfo.value.nowTime;

        let timt;
        timt = setInterval(() => {
          data.TimeNum++;
          if (data.TimeNum == codeinfo.value.endTime) {
            clearInterval(timt);
            gettick();
          }
        }, 1000);
      }
    };
    let clean = () => {
      clearInterval(data.time);
    };
    onMounted(() => {
      console.log(codeinfo.value);
      let nowTime = new Date().getTime() / 1000;
      if (!codeinfo.value || Math.floor(nowTime) > codeinfo.value.endTime) {
        gettick();
      } else {
        data.qrcodeVal = codeinfo.value.url;
        data.time = setInterval(() => {
          // getscantick(codeinfo.value.scene_str);
        }, data.times);
      }
      watchTime();
    });
    onUnmounted(() => {
      clearInterval(data.time);
    });
    return {
      clean,
      loginSuccess,
      watchTime,
      getscantick,
      bindcodebox,
      codeinfo,
      gettick,
      ...toRefs(data),
    };
  },
});
</script>
<style lang="scss" scoped></style>
